package application;

import zhengqc.stack.ArrayStack;
import zhengqc.stack.Stack;

public class BracketsMatching {
    private char[] brackets;
    public BracketsMatching() {}
    public void setBrackets(String bracketsString) {
        this.brackets = bracketsString.toCharArray();
    }
    public void setBrackets(char[] brackets) {
        this.brackets = brackets;
    }
    public boolean isValid() {
        Stack<Character> stack = new ArrayStack<>();
        for (char c: this.brackets) {
            if ( c == '(' || c == '[' || c == '{' ) {
                stack.push(c);
            } else {
                if (c != ')' && c != ']' && c != '}') {
                    continue;
                }
                if ( stack.isEmpty() ) {
                    return false;
                }
                char topChar = stack.pop();
                if ( (c == ')' && topChar != '(') ||
                     (c == ']' && topChar != '[') ||
                     (c == '}' && topChar != '{')) {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }
}
